--File ： ChapterView.lua
--Author : zee
--Date   : 2015/4/8
--Description : 关卡主界面 chapter.fla 第一帧




BHero = class("BHero",function()
    -- 继承
    local ret = cc.Node:create()

    -- 大弓箭，需要记录的
    ret.bigArrowNode = nil
    ret.bigArrowJianNode = nil 
    ret.bigArrowCenter = nil

    return ret
end)

BHero.__index = BHero


function BHero:create()
    local ret = BHero:new()
    ret:init()
    return ret
end











--------------------------------------------------------
-- 初始化
--------------------------------------------------------
function BHero:init()
    self.img = cc.Sprite:create("img/hero_1.png")
    self:addChild( self.img )
    -- 读取英雄属性。
    self:initData()
    -- 增加到碰撞组
    CrashCenter.__instance:addCrashObj( self, G_Crash_Hero , cc.size(50,50) )
end


-- 初始化属性数据。
function BHero:initData()
    self.speed = 400.0
    self.rushSpeed = 50000.0
    self.rushDistance = 160.0

end


function BHero:onFrame(dt)
    local x,y = self:getPosition()
    --self:setPosition( x, y+1 )
end



--------------------------------------------------------
-- 转换方法
--------------------------------------------------------
function BHero:pointFromTouch( poi_touch )
end


--------------------------------------------------------
-- 操控展示
--------------------------------------------------------
function BHero:moveToMap( poi )
    local a = cc.p( self:getPosition() )
    local dis = distance( poi, a)
    local time = dis/self.speed
    local mov = cc.MoveTo:create(time, poi)
    self:stopAllActions()
    self:runAction( mov )
end


function BHero:rushToMap( fromPoi, toPoi )
    local a = cc.p( self:getPosition() )
    local to = cc.p( toPoi.x - fromPoi.x , toPoi.y - fromPoi.y )
    local dis = distance( to, cc.p(0,0))
    local time = self.rushDistance/self.rushSpeed 
    local rate = self.rushDistance / dis

    to.x = to.x * rate
    to.y = to.y * rate
    local nowDis = distance(cc.p(0,0), to)
    cclook("现在 瞬移了 %s 的距离", nowDis );

    local mov = cc.MoveBy:create(time, to)
    self:stopAllActions()
    self:runAction( mov )
end


function BHero:jumpToMap( poi )
    local fade = cc.FadeOut:create(0.05)
    local move = cc.MoveTo:create( 0.05, poi)
    local recov = cc.FadeIn:create(0.05)
    local seq = cc.Sequence:create( fade, move, recov )
    cclook(" 瞬移连续跳跃不太合理，可以把这种移动方式放到别的游戏中，%s, %s," ,
        "这个可以更换成无需同时按压屏幕，而是左右互点频繁时，增加跳跃层数，",
        "出现长时间的迟钝时，释放跳跃层级。" )
    -- self:stopAllActions()
    -- self:runAction( seq )
    -- 跳跃应该去掉，放到别的游戏中去，这样容易干扰操作，不合理。
end


function BHero:fire_arrow( poi )
    local arrow = cc.Sprite:create("img/arrow.png")
    self:getParent():addChild(arrow);
    local nowPoi = cc.p( self:getPosition() )

    arrow:setPosition( cc.p(self:getPosition() ) )
    local nowDis = distance( nowPoi, poi)
    local rate = G_Arrow_Distance/nowDis
    local byPoi = cc.p( (poi.x - nowPoi.x)*rate, (poi.y - nowPoi.y)*rate )
    local time = G_Arrow_Distance/G_Arrow_Speed

    local moveBy = cc.MoveBy:create(time, byPoi)
    local function evt()
        arrow:removeFromParent()
    end
    local cb = cc.CallFunc:create( evt )
    local seq = cc.Sequence:create( moveBy, cb)
    arrow:runAction( seq )
    -- 获取角度
    local ang = getAngle(nowPoi, poi)
    arrow:setRotation( -ang )
    cclook(" 射出去的角度 :%s" , ang)
    -- 暂时箭太大了，需要做缩小
    arrow:setScale(0.3);
end



-----------------------------------------------------
-- 寒冰的大弓箭技能
function BHero:bigArrowStart( p1, p2)
    
    if self.bigArrowNode==nil then 
        -- 英雄本身要停滞移动
        self:stopAllActions();
        -- 
        self.bigArrowCenter = cc.p( (p2.x-p1.x), (p2.y-p1.y))
        -- 弓
        self.bigArrowNode = cc.Sprite:create("img/bigGong.png")
        self:getParent():addChild( self.bigArrowNode )
        self.bigArrowNode:setLocalZOrder( 1 )
        self.bigArrowNode:setPosition( self:getPosition() );
        -- 箭
        self.bigArrowJianNode = cc.Sprite:create("img/bigArrow.png");
        self:getParent():addChild( self.bigArrowJianNode )
        self.bigArrowJianNode:setLocalZOrder( 0 )
        self.bigArrowJianNode:setPosition( self:getPosition() );

        cclook(" 开始射搭建 ")
    end
end

function BHero:bigArrowMove( p1, p2 )
    if (self.bigArrowNode ~= nil ) then 
        local pCenter = cc.p(  p1.x+(p2.x-p1.x)/2 , p1.y+(p2.y-p1.y)/2 )
        local oriPoi = cc.p( self:getPosition() )
        local angle = getAngle( pCenter, oriPoi  )
        self.bigArrowNode:setRotation( -angle )
        self.bigArrowJianNode:setRotation( -angle )

        -- 
        local sp = cc.Sprite:create("img/card_1.png")
        sp:setScale(0.1)
        sp:setPosition( pCenter )
        self:getParent():addChild(sp)
        local del = cc.DelayTime:create(1)
        local function rm()
            sp:removeFromParent()
        end
        local cb = cc.CallFunc:create(rm)
        local seq = cc.Sequence:create( del, cb )
        sp:runAction( seq )

        cclook(" 弓箭的角度: %s", angle )
    end
end


function BHero:fire_bigArrow( p1, p2 )
    cclook(" 射大剑结束 ")
    if( self.bigArrowNode~=nil )then 
        -- 基座借住
        local nodeBase = cc.Node:create()
        self.bigArrowJianNode:getParent():addChild( nodeBase )
        nodeBase:setPosition( self.bigArrowJianNode:getPosition() )
        nodeBase:setLocalZOrder( self.bigArrowJianNode:getLocalZOrder() )
        nodeBase:setRotation( self.bigArrowJianNode:getRotation() )
        -- 箭体
        local bigArrowJian_body = cc.Sprite:create("img/bigArrow.png")
        nodeBase:addChild( bigArrowJian_body)
        -- 箭体飞行。
        local time = G_Arrow_Distance/G_Arrow_Speed
        local movAct = cc.MoveBy:create( time, cc.p(0 + G_Arrow_Distance , 0) )
        local function bigArrowEnd()
            bigArrowJian_body:getParent():removeFromParent()
        end
        local bigArrowCB = cc.CallFunc:create( bigArrowEnd )
        local seq = cc.Sequence:create( movAct, bigArrowCB )
        bigArrowJian_body:runAction( seq )
        -- 弓和箭隐藏。 
        self.bigArrowJianNode:removeFromParent()
        self.bigArrowNode:removeFromParent()
        self.bigArrowCenter = nil
        self.bigArrowJianNode = nil
        self.bigArrowNode = nil
    end
end
-----------------------------------------------------



-- 幻影箭雨技能。
function BHero:fire_bigSkill( poiStart, poiEnd )
    cclook(" %s -- %s" , poiStart, poiEnd )
    -- 本身的位移 
    local my = cc.p( self:getPosition() )
    local dp = cc.p( poiEnd.x - poiStart.x , poiEnd.y - poiStart.y )
    local dis = distance( dp, cc.p(0,0))
    local time = dis/self.rushSpeed 
    local act = cc.MoveBy:create( time ,dp)
    self:runAction( act )
    -- 放技能时，主角暂时隐身。
    self:setVisible(false)

    -- 幻影影子。
    local copy = 9
    local rate = dis/copy
    for i=1 , copy do 
        local one = cc.Sprite:create("img/hero_1.png")
        one:setScale(0.8)
        one:setPosition( cc.p(  my.x + (poiEnd.x - poiStart.x)/copy*i,
                                my.y + (poiEnd.y - poiStart.y)/copy*i   )  )
        self:getParent():addChild(one)
        one:setVisible(false)
        -- 依次延时出现
        local function shoot()
            -- 幻影出现
            one:setVisible(true)
            one:setOpacity(100)
            -- 射箭
            local arrow = cc.Sprite:create("img/arrow.png")
            arrow:setScale(0.3)
            arrow:setRotation( -90 )
            one:getParent():addChild(arrow);
            arrow:setPosition( cc.p( one:getPosition() ) )
            local time = G_Arrow_Distance/G_Arrow_Speed
            local arrowMove = cc.MoveBy:create(time, cc.p(0, G_Arrow_Distance))
            local function arrowDie()
                arrow:removeFromParent()
            end
            local arrowAct = cc.CallFunc:create( arrowDie )
            local seq = cc.Sequence:create( arrowMove, arrowAct)
            arrow:runAction( seq )
        end
        -- 影子的延迟技能动作
        local showTime = 1.5/copy * i
        local oneShowAct = cc.DelayTime:create(showTime)
        local shootEvt = cc.CallFunc:create(shoot)
        local shootAgain = 0.3;
        local function oneDie()
            one:removeFromParent()
            if(i==9) then 
                -- 第九个幻影放完技能时，主角再现身
                -- self:setOpacity(255)
                self:setVisible(true)
            end
        end
        local oneDieCF = cc.CallFunc:create( oneDie )
        local shootCF_1 = cc.DelayTime:create( shootAgain + math.random(4,9)*0.01 )
        local shootCF_2 = cc.DelayTime:create( shootAgain + math.random(4,9)*0.01 )
        local shootCF_3 = cc.DelayTime:create( shootAgain + math.random(4,9)*0.01 )
        local oneSeq = cc.Sequence:create(   oneShowAct, shootEvt, 
                                                shootCF_1, shootEvt, 
                                                shootCF_2, shootEvt,
                                                shootCF_3, shootEvt,
                                                shootCF_3, oneDieCF )
        one:runAction( oneSeq )
    end
end









